<?php
    class Admin_m extends Model{
        function Admin_m(){
            parent::Model();
            $this->load->database();
        }
        function get_all_news($num,$offset)
        {
            $this->db->select('news.id,news.image,news.title,categories.name');
            $this->db->from('news');
            $this->db->join('categories', 'news.cat=categories.id');
            $this->db->limit($num,$offset);
            $this->db->order_by('news.date_post','DESC');
            $query = $this->db->get();
            return $query->result_array();
        }
        function get_News_By_ID($id){
            $this->db->select();
            $this->db->from('news');
            $this->db->where(array('id'=>$id));
            $query = $this->db->get();  
            return $query;   
        }
        function update_news($data,$id){
            $this->db->update('news',$data,array('id'=>$id));
        }
        function get_news_by_cat($cat_id){
            $this->db->select('id');
            $this->db->from('news');
            $this->db->where(array('cat'=>$cat_id));
            $query = $this->db->get();
            return $query->result_array();
        }
        function delete_news($id){
            return $this->db->delete('news',array('id'=>$id));    
        }
        function add_new_category($cat_name){
            $this->db->insert('categories',array('name'=>$cat_name));
        }
        function get_cat_by_id($id){
            $this->db->select();
            $this->db->from('categories');
            $this->db->where(array('id'=>$id));
            $query = $this->db->get();
            return $query;
        } 
        function update_cat($id,$data){
            $this->db->update('categories',$data,array('id'=>$id));
        }
        function get_all_categories($num,$offset){
            $this->db->select();
            $this->db->from('categories');
            $this->db->limit($num,$offset);
            $this->db->order_by('categories.name','DESC');
            $query = $this->db->get();
            return $query->result_array();
        }
        function delete_cat($id){
            $this->db->delete('categories',array('id'=>$id));
        }
        function post_news_data($data){
            $this->db->insert('news',$data);
        }
        /*User module*/
        function get_all_admin($per_page,$off_set){
            $this->db->select();
            $this->db->from('admins');
            $this->db->limit($per_page,$off_set);
            $query = $this->db->get();
            return $query->result_array();
        }
        function delete_admin($id){
            if($this->session->userdata('adminid')!=$id){
                return $this->db->delete('admins',array('id'=>$id));   
            }
        }
        function add_admin($data){
            return $this->db->insert('admins',$data);
        }
        function get_admin_by_ID($id){
            $this->db->select();
            $this->db->from('admins');
            $this->db->where(array('id'=>$id));
            $query = $this->db->get();  
            return $query;   
        }
        function update_admin($data,$id){
            return $this->db->update('admins',$data,array('id'=>$id));
        }
        
        function get_all_user($per_page,$off_set){
            $this->db->select();
            $this->db->from('user');
            $this->db->limit($per_page,$off_set);
            $query = $this->db->get();
            return $query->result_array();
        }
        function delete_user($id){
            return $this->db->delete('user',array('user_id'=>$id));
        }
        function get_user_by_id($id){
            return $this->db->get_where('user',array('user_id'=>$id));
        }
        function add_user($data){
            return $this->db->insert('user',$data);
        }
        function update_user($data,$id){
            return $this->db->update('user',$data,array('user_id'=>$id));
        }
        function login($data){
        	$query=$this->db->get_where('admins',$data);
        	return $query;
        }
        function update_profile($data,$id){
        	return $this->db->update('admins',$data,array('id'=>$id));
        }
        function is_password_match($id,$old_pass){
            $query= $this->db->get_where('admins',array('id'=>$id,'pass'=>$old_pass));
            if ($query->num_rows()>0){
                return TRUE;
            } return FALSE;
        }
        function change_pass($id,$new_pass){
            $this->db->where('id',$id);
            $this->db->update('admins',array('pass'=>$new_pass));
        }
        function get_all_site_info(){
            $this->db->from('site_settings');
            $query = $this->db->get();
            return $query->result_array();
        }
        function get_setting_info($setting_name){
            $this->db->from('site_settings');
            $this->db->where(array('setting_name'=>$setting_name));
            $query = $this->db->get();
            return $query;
        }
        function save_settings($data,$setting_name){
            return $this->db->update('site_settings',array('setting_info'=>$data),array('setting_name'=>$setting_name));
        }
    }
    
?>
